
/* Javascript to handle the behaviour of the web control for HelpPopup functionality*/

Type.registerNamespace('AJAXControls');

AJAXControls.HelpPopupBehavior = function(element) {

    AJAXControls.HelpPopupBehavior.initializeBase(this, [element]);

        this._entityName = '';
        this._attributeName = '';
        this._servicePath = null;
        this._serviceMethod = null;
        this._closeImageUrl = null;
        this._width = 200;
        this._fillColor = '#ccffcc';
        this._textColor = 'Black';
        this._closeCell = null;
        this._originalValidationMethod = null;
        this._validationMethodOverride = null;
        this._elementToValidate = null;
        this._popupTable = null;
        this._errorMessageCell = null;
        this._calloutArrowCell = null;
        this._closeImage = null;
        this._popupBehavior = null;
        this._focusAttached = false;
        this._isOpen = false;
        this._line = null;
        this._oldColor = null;
        this._closeClickHandler = Function.createDelegate(this, this._oncloseClick);
        this._focusHandler = Function.createDelegate(this, this._onFocus);
        this._blurHandler = Function.createDelegate(this, this._onBlur);
        
}

AJAXControls.HelpPopupBehavior.prototype = 
{

    //Property closeImageUrl
    get_closeImageUrl : function() 
    {
        return this._closeImageUrl;
    },
    set_closeImageUrl : function(value) 
    {

        if (this._closeImageUrl != value) {
            this._closeImageUrl = value;
            if (this.get_isInitialized()) {
                this._closeImage.src = value;
            }
            this.raisePropertyChanged("closeImageUrl");
        }
    },
    
    //Property serviceMethod
    get_serviceMethod: function() 
    {
        return this._serviceMethod;
    },
    set_serviceMethod: function(value) 
    {
        this._serviceMethod = value;
    },
    
    //Property entityName
    get_entityName: function() 
    {
       return this._entityName;
    },
    set_entityName: function(value) 
    {
        this._entityName = value;
    },
    
    //Property attributeName
    get_attributeName: function() 
    {
        return this._attributeName;
    },
    set_attributeName: function(value) 
    {
        this._attributeName = value;
    },
    
    //Property width
    get_width: function() 
    {
       return this._width;
    },
    set_width: function(value) 
    {
        this._width = value;
    },
   
    //Property fillColor
    get_fillColor: function() 
    {
        return this._fillColor;
    },
    set_fillColor: function(value) 
    {
        this._fillColor = value;
    },
    
    //Property textColor
    get_textColor: function() 
    {
        return this._textColor;
    },
    set_textColor: function(value) 
    {
       this._textColor = value;
    },
    
    //Property servicePath
    get_servicePath: function() 
    {
        return this._servicePath;
    },
    set_servicePath: function(value) 
    {
           this._servicePath = value;
    },

     
    initialize : function() {
        AJAXControls.HelpPopupBehavior.callBaseMethod(this, 'initialize');

        var elt = this.get_element();
       var popupTableBody = document.createElement("tbody");
        var popupTableRow = document.createElement("tr");
        var calloutCell = document.createElement("td");
        var calloutTable = document.createElement("table");
        var calloutTableBody = document.createElement("tbody");
        var calloutTableRow = document.createElement("tr");
        var closeCell = this._closeCell = document.createElement("td");
        var popupTable = this._popupTable = document.createElement("table");
        var calloutArrowCell = this._calloutArrowCell = document.createElement("td");
        var closeImage = this._closeImage = document.createElement("img");
        var errorMessageCell = this._errorMessageCell = document.createElement("td");
	  
	    // popupTable
	   
	    popupTable.cellPadding = 0;
        popupTable.cellSpacing = 0;
        popupTable.border = 0;
        popupTable.width = this.get_width();
        popupTable.style.display = 'none';
   
        // popupTableRow
             
        popupTableRow.vAlign = 'top';
        popupTableRow.style.height = "100%";
    
        // calloutCell
   
        calloutCell.width = 20;
        calloutCell.align = "right";
        calloutCell.style.height = "100%";
        calloutCell.style.verticalAlign = "top";
     
        // calloutTable
       
        calloutTable.cellPadding = 0;
        calloutTable.cellSpacing = 0;
        calloutTable.border = 0;
        calloutTable.style.height = "100%";
   
        // _calloutArrowCell
        
        calloutArrowCell.align = "right";
        calloutArrowCell.vAlign = "top";
        calloutArrowCell.style.fontSize = "1px";
        calloutArrowCell.style.paddingTop = "2px";
       
        // _errorMessageCell
       
        errorMessageCell.style.backgroundColor = this.get_fillColor();
        errorMessageCell.style.fontFamily = 'verdana';
        errorMessageCell.style.fontSize = '10px';
        errorMessageCell.style.color = this.get_textColor();
        errorMessageCell.style.padding = "5px";
        errorMessageCell.style.borderTop = "0px solid Green";
        errorMessageCell.style.borderLeft = "0px solid Green"; 
        errorMessageCell.style.borderBottom = "0px solid Green";
        errorMessageCell.width = '100%';
        errorMessageCell.innerHTML = 'No help available.';
   
        // closeCell
  
        closeCell.style.borderTop = "0px solid Green";
        closeCell.style.borderRight = "0px solid Green";
        closeCell.style.borderBottom = "0px solid Green";
        closeCell.style.backgroundColor = this.get_fillColor();
        closeCell.style.verticalAlign = 'top';
        closeCell.style.textAlign = 'right';
        closeCell.style.padding = '2px';

        // closeImage
            
        closeImage.src = this.get_closeImageUrl();
        closeImage.style.cursor = 'pointer';
     
        // Create the DOM tree

        elt.parentNode.appendChild(popupTable)
        popupTable.appendChild(popupTableBody);
        popupTableBody.appendChild(popupTableRow);
        popupTableRow.appendChild(calloutCell);
        calloutCell.appendChild(calloutTable);
        calloutTable.appendChild(calloutTableBody);
        calloutTableBody.appendChild(calloutTableRow);
        calloutTableRow.appendChild(calloutArrowCell);
        popupTableRow.appendChild(errorMessageCell);
        popupTableRow.appendChild(closeCell);
        closeCell.appendChild(closeImage);
    
        // initialize callout arrow
     
        var div = document.createElement("div");
        div.style.fontSize = "1px";
        div.style.position = "relative";
        div.style.left = "0px";
        div.style.borderTop = "0px solid Green";
        div.style.width = "15px";
        calloutArrowCell.appendChild(div);        
        for(var i = 14; i > 0; i--)
        {
            var line = this._line = document.createElement("div");
            line.style.width = i.toString() + "px";
            line.style.height = "1px";
            line.style.overflow = "hidden";
            line.style.backgroundColor = this.get_fillColor();
            line.style.borderLeft = "0px solid Green";
            div.appendChild(line);
        }
         
       this._popupBehavior = $create(
          AjaxControlToolkit.PopupBehavior,{positioningMode : 0, parentElement : this.get_element()}, { }, null,this._popupTable);

        $addHandler(this._closeImage, "click", this._closeClickHandler);
        $addHandler(this.get_element(), "blur", this._blurHandler);
        $addHandler(this.get_element(), 'focus',this._focusHandler);

    },   
   
    // Function dispose
    dispose : function() 
    {
        if (this._popupBehavior) 
        {
            this._popupBehavior.dispose();
            this._popupBehavior = null;
        }
        
        var element = this.get_element();
        if(element) 
        {
            $removeHandler(element, "focus", this._focusHandler);
            $removeHandler(element, "blur", this._blurHandler);
        }
        element = this._closeImage;
        
        if(element) 
        {
            $removeHandler(element, "click", this._closeClickHandler);
        }
        
        AJAXControls.HelpPopupBehavior.callBaseMethod(this, 'dispose');
    },

   
     
    // Function _onFocus
    _onFocus : function(e)
    {
       
        Sys.Net.WebServiceProxy.invoke(this.get_servicePath(), this.get_serviceMethod(), false,
                                        {entity : this.get_entityName(), attribute : this.get_attributeName()},
                                        Function.createDelegate(this, this._onMethodComplete),
                                        Function.createDelegate(this, this._onMethodFailed),
                                        "User Context"); 
            
        this._popupBehavior.set_x(CommonToolkitScripts.getSize(this.get_element()).width);
        this._popupBehavior.show();

    },

    // Function _onBlur
    _onBlur : function(e)
    {
        this._hide();
    },

    // Function _onMethodComplete
    _onMethodComplete: function(result, context, methodName) 
    {
       this._errorMessageCell.innerHTML = result;
    },
    
    // Function _onMethodFailed
    _onMethodFailed: function(err, response, context) 
    {
     //No operation  
    },
    
    // Function _oncloseClick
    _oncloseClick : function(e) 
    {
        this._hide();
    },

    // Function hide
    _hide : function() 
    {
         this._popupBehavior.hide();
    }
    
}

AJAXControls.HelpPopupBehavior.descriptor = {
    properties: [   {name: 'closeImageUrl', type: String},
                    {name: 'serviceMethod', type: String},
                    {name: 'servicePath', type: String},
                    {name: 'entityName', type: String},
                    {name: 'attributeName', type: String},
                    {name: 'fillColor', type: String},
                    {name: 'width', type: Number}, 
                    {name: 'displayControl', type: String},
                    {name: 'textColor', type: String}]
      }
      
AJAXControls.HelpPopupBehavior.registerClass('AJAXControls.HelpPopupBehavior', AjaxControlToolkit.BehaviorBase);
